var introint = 0;
var introcur = 0;
var isintro = 1;
var initialSet = 1;
var introseq = [3,1,5,4,2];

var menuwidth = [];
var tmenuwidth = [];
var submenuwidth = [];
var tsubmenuwidth = [];

var countrybutlist = ["New York","Tangier","Tetouan","Algiers","Dubai","New Delhi","Beijing","Shanghai","Guangzhou","Bangkok","Phuket","Bangalore","Kuala Lumpur","Maldives","Malacca","Singapore","Jakarta","Bali","Mauritius","Gold Coast","Melbourne","Abu Dhabi","Marassi", "Turks & Caicos","Surabaya","Rishikesh","Provence","Hyderabad","Mumbai","Hong Kong","Hainan"];
var locationiconlist = [];
countrybutlist.sort();

var gallerycur = 0;
var galleryamt = 0;
var gallerybutgrp = 7;
var gallerybutgrpamt = 0;
var gallerycurgrp = 0;
var gallerytimeInt = 0;
var isinnergallery = 0;
var projecttypelist = ['houses','hospitality','commercial','condominiums','masterplanning','interiors'];
var scrollpage = 0;
var isscrolling = 0;
var jobpage = 0;
var locationflag= 0;
var newsflag = 0;

var xdata;
var newsdata;
var mainimgpath = 'images/';

function navigationShift(pos){
	var curlocx = 0;
	$('#bottomlevelnavi .lvl1navi').each(function(){
		var curpos = $(this).index();
		$(this).stop(true).animate({'left':curlocx},500);
		curlocx += menuwidth[curpos];
		if(pos == curpos){
			curlocx += submenuwidth[curpos];
		}
	});
	$('#bottomlevelnavi').stop(true).animate({
		'margin-left': 916-curlocx
	},500);
}
function setupNavPanels(layer){
	$(layer).find('.gridbutton').each(function(){
		if(!$(this).hasClass('disabled')){
			$(this).hover(function(){
				$(this).find('.gridbg').stop(true,true).fadeIn(300);
			},function(){
				if(!($(this).hasClass('gridactive'))){
					$(this).find('.gridbg').stop(true,true).fadeOut(300);
				}
			});
			
			$(this).click(function(){
				$(this).siblings().removeClass('gridactive');
				$(this).siblings().find('.gridbg').stop(true,true).fadeOut(300);
				$(this).addClass('gridactive');
				var pos = $(this).index();
				gridClick(layer,pos);
			});
		
		}
	});

}

function gridClick(layer,pos){
	var from = layer.replace(' .gridbutton_container','');

	switch(from) {
		case '#page_profile':
			loadText(from,pos);
			break;
		case '#page_people':
			if(pos != 0){
				loadGallery(from,pos,true);
			} else if(pos == 0){
				loadText(from,pos);
			}
			break;
			
		case '#page_careers':
			loadText(from,pos);
			break;
			
		case '#page_projecttype':

			var datagrp = [];
			$(xdata).find('project').each(function(){
				if($(this).find('category').text() == projecttypelist[pos]){
					datagrp.push($(this).index());
				}
			});
			var thumb = '<div class="thumbnails_container">';
			for(var i=1;i<=datagrp.length;i++){
				var additionalclass = '';
				if($(xdata).find('project').eq(datagrp[i-1]).find('button').attr('placeholder') == 'highlight'){
					additionalclass=  ' placeholder';
				}
			
				thumb += '<div id="p_'+datagrp[i-1]+'" class="thumbnail_panel '+additionalclass+'"><img src="'+mainimgpath+'architecture/sm_thumbnail/'+$(xdata).find('project').eq(datagrp[i-1]).find('button').text()+'"/><div class="thumbnail_hover"><img src="'+mainimgpath+'architecture/gs_thumbnail/'+$(xdata).find('project').eq(datagrp[i-1]).find('button').text()+'"/></div></div>';
			}
			thumb += '</div>';
			
			$('#page_projecttype .content_panel').html(thumb);
			
			$('.thumbnail_hover img').animate({opacity:0.8},50);
			
			$('.thumbnail_panel').each(function(){
				$(this).stop(true,true).delay($(this).index()*100).fadeIn(800);
			});
			
			$('.thumbnail_panel').hover(function(){
				$('#page_projecttype .gridbutton_container').stop(true).fadeOut(800);
				$(this).find('.thumbnail_hover').stop(true,true).fadeOut(400);
				
				var gindex = parseInt($(this).attr('id').split('_')[1]);
				var ntitle = $(xdata).find('project').eq(gindex).find('title').text();
				$('#bottomlefttext').stop(true,true).hide();
				$('#bottomlefttext').html(ntitle);
				$('#bottomlefttext').stop(true,true).fadeIn(300);
			},function(){
				$('#page_projecttype .gridbutton_container').stop('',true,true).delay(300).fadeIn(800);
				$(this).find('.thumbnail_hover').stop(true,true).fadeIn(400);
				if(isinnergallery == 0){
					$('#bottomlefttext').stop(true,true).hide();
				}
			});	
		
			$('.thumbnail_panel').click(function(){
				if(!$(this).hasClass('placeholder')) {
					var gindex = parseInt($(this).attr('id').split('_')[1]);
					isinnergallery = 1;
					loadGallery(from,gindex,false);
				}
			});
			
			break;
			
		case '#page_location':
			
			$('#page_location').stop(true,true).show();
			$('#page_locationmap').stop(true,true).fadeOut();
			
			var datagrp = [];
			$(xdata).find('project').each(function(){
				if($(this).find('location').text() == countrybutlist[pos]){
					datagrp.push($(this).index());
				}
			});
			var thumb = '<div class="thumbnails_container">';
			for(var i=1;i<=datagrp.length;i++){
				var additionalclass = '';
				if($(xdata).find('project').eq(datagrp[i-1]).find('button').attr('placeholder') == 'highlight'){
					additionalclass=  ' placeholder';
				}
			
				thumb += '<div id="p_'+datagrp[i-1]+'" class="thumbnail_panel '+additionalclass+'"><img src="'+mainimgpath+'architecture/sm_thumbnail/'+$(xdata).find('project').eq(datagrp[i-1]).find('button').text()+'"/><div class="thumbnail_hover"><img src="'+mainimgpath+'architecture/gs_thumbnail/'+$(xdata).find('project').eq(datagrp[i-1]).find('button').text()+'"/></div></div>';
			}
			thumb += '</div>';
			
			$('#page_location .content_panel').html(thumb);
			$('#locationtitle').html(countrybutlist[pos]);
			$('.thumbnail_hover img').animate({opacity:0.8},50);
			
			$('.thumbnail_panel').each(function(){
				$(this).stop(true,true).delay($(this).index()*100).fadeIn(800);
			});
			
			$('.thumbnail_panel').hover(function(){
				$('#page_location .gridbutton_container').stop(true).fadeOut(800);
				$(this).find('.thumbnail_hover').stop(true,true).fadeOut(400);
				
				var gindex = parseInt($(this).attr('id').split('_')[1]);
				var ntitle = $(xdata).find('project').eq(gindex).find('title').text();
				$('#bottomlefttext').stop(true,true).hide();
				$('#bottomlefttext').html(ntitle);
				$('#bottomlefttext').stop(true,true).fadeIn(300);
			},function(){
				$('#page_location .gridbutton_container').stop('',true,true).delay(300).fadeIn(800);
				$(this).find('.thumbnail_hover').stop(true,true).fadeIn(400);
				if(isinnergallery == 0){
					$('#bottomlefttext').stop(true,true).hide();
				}
			});	
		
			$('.thumbnail_panel').click(function(){
				if(!$(this).hasClass('placeholder')) {
					var gindex = parseInt($(this).attr('id').split('_')[1]);
					isinnergallery = 1;
					loadGallery(from,gindex,false);
				}
			});
			
			break;
	}
}

//XMLs
function loadNews(){
	$.ajax({
		type: "GET",
		url: 'xmldata/profile.xml',
		dataType: "xml",
		success: function(xmldata) {
			newsdata = xmldata;
			newsflag = 1;
		}
	});
}


function loadXML(div,xml,type) {
	//note that different xml is structured differently
	$.ajax({
		type: "GET",
		url: xml,
		dataType: "xml",
		success: function(xmldata) {
			switch(type) {
				case 'profile':
				case 'people':
				case 'projecttype':
				case 'location':
					xdata = xmldata;
					$(div).stop(true,true).fadeIn();
					
					if(type == 'location'){
						 locationflag = 1;
						 $('.locationicon').each(function(){
							$(this).stop(true,true).hide();
							$(this).css('left', '-200px');
							$(this).delay($(this).index('.locationicon')*30).fadeIn(600);
							$(this).delay($(this).index('.locationicon')*30).animate({left:locationiconlist[$(this).index('.locationicon')]},400);
						 });
					}
					
					break;

				case 'careers':
					xdata = xmldata;
					$(div).find('.content_panel').empty();
					gridClick(div,0);
					$('#page_careers .gridbutton').eq(0).addClass('gridactive');
					$('#page_careers .gridbutton').eq(0).find('.gridbg').stop(true,true).show();
					$(div).stop(true,true).fadeIn();
					break;
				case 'interior':
				case 'collection':
				case 'landscape':
					galleryamt = $(xmldata).find('collection').find('pic').length;
					gallerybutgrpamt = Math.ceil(galleryamt/gallerybutgrp);
					gallerycurgrp = 1;
					var imgpath = $(xmldata).find('collection').attr('path');
					var addin = '';
					var butin = '<div class="gallbut_left"></div><div class="gallbut_grp">';
					for(var i =1;i<=galleryamt;i++){
						addin+= '<div class="galleryimg"><img src="'+imgpath+$(xmldata).find('collection').find('pic').eq(i-1).text()+'"/></div>';
						butin += '<span class="gallerybut">'+i+'</span>';
					}				
					butin += '</div><div class="gallbut_right"></div><div class="gallbut_pause"></div><div class="gallbut_play"></div>';
					var gallin = '<div class="gallery_left"><div class="gallbutcontainer">'+butin+'</div></div><div class="gallery_right">'+addin+'</div>';				
					$(div).html(gallin);
					setGalleryButtons();
					galleryShift(1);
					startGallery();
					$('#page_gallery').stop(true,true).fadeIn();
					break;
			}
		}
	});
}
function loadText(from,pos){
	var defaultbutset = 0;
	switch(from){
		case '#page_profile':
			switch(pos){
				case 0:
					var texttoload = $(xdata).find('firm').text();
					var addin = '<div class="contenttext_left"><div class="content_close"></div></div><div class="contenttext_right">'+texttoload+'</div>';
					$('#page_innertext').html(addin);
					defaultbutset = 1;
					break;
				case 2:
					var texttoload = $(xdata).find('awards').find('international').text();
					var texttoload2 = $(xdata).find('awards').find('regional').text();
					var addin = '<div class="contenttext_left"><div class="left_links"><p><a class="inpagelinks" href="#international">International</a><br/><a class="inpagelinks" href="#regional">Regional</a></p></div><div class="content_close"></div></div><div class="contenttext_right"><div class="inpagecontent">'+texttoload+'</div><div class="inpagecontent">'+texttoload2+'</div></div>';
					$('#page_innertext').html(addin);
					
					$('.inpagecontent').each(function(){

						if($(this).find('.textscroll').size()>0){
							//calculate size
							//if($(this).find('.textscroll').height() > 240){
								$(this).append('<div class="pagescroll_top"></div><div class="pagescroll_bottom"></div>');
								scrollpage = 1;
								$(this).find('.textscroll').css('position','absolute');
								$(this).find('.textscroll').css('top','0');
								$('.pagescroll_top').click(function(){
									if(isscrolling == 0){
										//alert($(this).siblings('.textscroll').height());
										var maxsh = Math.ceil($(this).siblings('.textscroll').height()/240);
										var newsh = scrollpage-1;
										if(newsh < 1){
											newsh = 1;
										}
										if(newsh != scrollpage){
											scrollpage = newsh;
											var newpos = 0- ((newsh-1)*240);
											isscrolling = 1;
											$(this).siblings('.textscroll').animate({
												top: newpos
											},500,function(){
												isscrolling = 0;
											});
										}
									}
								});
								$('.pagescroll_bottom').click(function(){
									if(isscrolling == 0){
										var maxsh = Math.ceil($(this).siblings('.textscroll').height()/240);
										var newsh = scrollpage+1;
										if(newsh > maxsh){
											newsh = maxsh;
										}
										if(newsh != scrollpage){
											scrollpage = newsh;
											var newpos = 0- ((newsh-1)*240);
											isscrolling = 1;
											$(this).siblings('.textscroll').animate({
												top: newpos
											},500,function(){
												isscrolling = 0;
											});
										}
									}
								});
							//}
						}
					});
					
					
					
					defaultbutset = 1;
					
					$('.inpagelinks').click(function(){
						var index = $(this).index('.inpagelinks');
						$('.inpagelinks').removeClass('active');
						$(this).addClass('active');
						$('.inpagecontent').stop(true,true).hide();
						$('.inpagecontent').eq(index).stop(true,true).fadeIn();
						return false;
					});
					$('.inpagelinks').eq(0).addClass('active');
					$('.inpagecontent').eq(0).stop(true,true).show();
					break;
				case 4:
					var texttoload = $(xdata).find('publications').find('current').text();
					var texttoload2 = $(xdata).find('publications').find('archive').text();
					var addin = '<div class="contenttext_left"><div class="left_links"><p><a class="inpagelinks" href="#current">Current</a><br/><a class="inpagelinks" href="#archive">Archive</a></p></div><div class="content_close"></div></div><div class="contenttext_right"><div class="inpagecontent">'+texttoload+'</div><div class="inpagecontent">'+texttoload2+'</div></div>';
					$('#page_innertext').html(addin);

					
					
					defaultbutset = 1;
					
					$('.inpagelinks').click(function(){
						var index = $(this).index('.inpagelinks');
						$('.inpagelinks').removeClass('active');
						$(this).addClass('active');
						$('.inpagecontent').stop(true,true).hide();
						$('.inpagecontent').eq(index).stop(true,true).fadeIn();
						return false;
					});
					$('.inpagelinks').eq(0).addClass('active');
					$('.inpagecontent').eq(0).stop(true,true).show();
					break;
				
				case 5:
					var currentsize = $(xdata).find('news').find('current').find('node').size();
					var archivesize = $(xdata).find('news').find('archive').find('node').size();
					var texttoload = '';
					var texttoload2 = '';
					
					for(var i=1;i<=currentsize;i++){
						texttoload += '<a href="'+$(xdata).find('news').find('current').find('node').eq(i-1).find('link').text()+'" target="_blank">'+$(xdata).find('news').find('current').find('node').eq(i-1).find('title').text()+'</a>';
						if(i != currentsize){
							texttoload += '<br/>';
						}
					}
					for(var z=1;z<=archivesize;z++){
						texttoload2 += '<a href="'+$(xdata).find('news').find('archive').find('node').eq(z-1).find('link').text()+'" target="_blank">'+$(xdata).find('news').find('archive').find('node').eq(z-1).find('title').text()+'</a>';
						if(z != archivesize){
							texttoload2 += '<br/>';
						}
					}
					
					var addin = '<div class="contenttext_left"><div class="left_links"><p><a class="inpagelinks" href="#current">Current</a><br/><a class="inpagelinks" href="#archive">Archive</a></p></div><div class="content_close"></div></div><div class="contenttext_right"><div class="inpagecontent">'+texttoload+'</div><div class="inpagecontent">'+texttoload2+'</div></div>';
					$('#page_innertext').html(addin);
					defaultbutset = 1;
					
					$('.inpagelinks').click(function(){
						var index = $(this).index('.inpagelinks');
						$('.inpagelinks').removeClass('active');
						$(this).addClass('active');
						$('.inpagecontent').stop(true,true).hide();
						$('.inpagecontent').eq(index).stop(true,true).fadeIn();
						return false;
					});
					$('.inpagelinks').eq(0).addClass('active');
					$('.inpagecontent').eq(0).stop(true,true).show();
				
				
					break;
					
			} 
			break;
		case '#page_people':
			if(pos == 0){
				var texttoload = $(xdata).find('principal').text();
				var addin = '<div class="contenttext_left"><div class="content_close"></div></div><div class="contenttext_right">'+texttoload+'</div>';
				$('#page_innertext').html(addin);
				defaultbutset = 1;
				
			}
			break;
		case '#page_careers':
			if(pos ==0){
				var texttoload = $(xdata).find('front').find('info').text();
				var addin = '<div class="careerfrontpanel">'+texttoload+'</div>';
				$('#page_careers .content_panel').empty();
				$('#page_careers .content_panel').hide();
				$('#page_careers .content_panel').html(addin);
				$('#page_careers .content_panel').stop(true,true).fadeIn();
			} else {
				$('#page_careers .content_panel').empty();
				$('#page_careers .content_panel').stop(true,true).hide();
				var jobsize = $(xdata).find('career').find('job').size();
				var jobpages = Math.ceil(jobsize/2);
				var addin = '<div class="careerjobpanel">';
				for(var i =1;i<=jobsize;i++){
					addin += '<div class="jobpanel">'+$(xdata).find('career').find('job').eq(i-1).text()+'</div>';
				}
				//add in buttons
				if(jobpages > 1){
					var butin = '';
					for(var z=1;z<=jobpages;z++){
						butin += '<div class="jobbut">'+z+'</div>';
					}
					addin += '<div class="jobbuttoncontainer"><div class="jobbut_left"></div>'+butin+'<div class="jobbut_right"></div></div>';
				}
				addin += '</div>';
				
				$('#page_careers .content_panel').html(addin);
				$('.jobpanel:even').css('margin-right', '20px');
				$('.jobpanel').hide();
				$('.jobpanel').eq(0).show();
				$('.jobpanel').eq(1).show();
				
				jobpage = 1;
				$('.jobbut_left').hover(function(){
					$(this).css('background-position', 'top right');
				},function(){
					$(this).css('background-position', 'top left');
				});
				$('.jobbut_left').click(function(){
					var newjob = jobpage -1;
					if(newjob < 1){
						newjob = 1;
					}
					if(newjob != jobpage){
						$('.jobpanel').stop(true,true).hide();
						$('.jobpanel').eq((newjob-1)*2).fadeIn();
						$('.jobpanel').eq(((newjob-1)*2)+1).fadeIn();
						$('.jobbut').removeClass('jobactive');
						$('.jobbut').eq(newjob-1).addClass('jobactive');
						jobpage = newjob;
					}
				});
				$('.jobbut_right').hover(function(){
					$(this).css('background-position', 'top right');
				},function(){
					$(this).css('background-position', 'top left');
				});
				$('.jobbut_right').click(function(){
					var newjob = jobpage +1;
					if(newjob > jobpages){
						newjob = jobpages;
					}
					if(newjob != jobpage){
						$('.jobpanel').stop(true,true).hide();
						$('.jobpanel').eq((newjob-1)*2).fadeIn();
						$('.jobpanel').eq(((newjob-1)*2)+1).fadeIn();
						$('.jobbut').removeClass('jobactive');
						$('.jobbut').eq(newjob-1).addClass('jobactive');
						jobpage = newjob;
					}
				});
				$('.jobbut').hover(function(){
					$(this).addClass('jobhover');
				},function(){
					$(this).removeClass('jobhover');
				});
				$('.jobbut').click(function(){
					var index = $(this).index('.jobbut') +1;
					if(index != jobpage){
						$('.jobpanel').stop(true,true).hide();
						$('.jobpanel').eq((index-1)*2).fadeIn();
						$('.jobpanel').eq(((index-1)*2)+1).fadeIn();
						$('.jobbut').removeClass('jobactive');
						$('.jobbut').eq(index-1).addClass('jobactive');
						jobpage = index;
					}
				});
				
				
				$('#page_careers .content_panel').stop(true,true).fadeIn();
			}
			break;
	}
	if(defaultbutset == 1){
		$('.content_close').click(function(){
				$('#page_innertext').empty();
				$('#page_innertext').stop(true,true).fadeOut(500,function(){ });
		});
	}
	
	if(from != '#page_careers'){
		$('.gridbutton').removeClass('gridactive');
		$('#page_innertext').stop(true,true).fadeIn();
	}
}


function loadGallery(from,pos,closegall){
	switch(from) {
		case "#page_people":
			var ff = 'people';
			if(pos == 1){
				ff = 'studio';
			}
			galleryamt = $(xdata).find(ff).find('pic').length;
			gallerybutgrpamt = Math.ceil(galleryamt/gallerybutgrp);
			gallerycurgrp = 1;
			var imgpath = 'images/peoplestudio/';
			var addin = '';
			var butin = '<div class="gallbut_left"></div><div class="gallbut_grp">';
			for(var i =1;i<=galleryamt;i++){
				addin+= '<div class="galleryimg"><img src="'+imgpath+$(xdata).find(ff).find('pic').eq(i-1).text()+'"/></div>';
				butin += '<span class="gallerybut">'+i+'</span>';
			}				
			butin += '</div><div class="gallbut_right"></div><div class="gallbut_pause"></div><div class="gallbut_play"></div><div class="gallbut_close"></div>';
			var gallin = '<div class="gallery_left"><div class="gallbutcontainer">'+butin+'</div></div><div class="gallery_right">'+addin+'</div>';				
			$('#page_gallery').html(gallin);
			
			$('.gridbutton').removeClass('gridactive');
			setGalleryButtons();
			galleryShift(1);
			startGallery();
			$('#page_gallery').stop(true,true).fadeIn();
			break;
		case "#page_projecttype":
			
			galleryamt = $(xdata).find('project').eq(pos).find('gallery').find('pic').length;
			gallerybutgrpamt = Math.ceil(galleryamt/gallerybutgrp);
			gallerycurgrp = 1;
			var imgpath = mainimgpath+'architecture/gallery/';
			var bigimgpath = mainimgpath+'architecture/gallery/big/';
			var addin = '';
			var butin = '<div class="gallbut_left"></div><div class="gallbut_grp">';
			for(var i =1;i<=galleryamt;i++){
				if($(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).attr('bigimage') == 'yes'){
					addin+= '<div class="galleryimg"><a rel="prettyPhoto[projecttype]" href="'+bigimgpath+$(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).text()+'" ><img src="'+imgpath+$(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).text()+'" alt="'+$(xdata).find('project').eq(pos).find('title').text()+'"/></a></div>';
				} else {
					addin+= '<div class="galleryimg"><img src="'+imgpath+$(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).text()+'"/></div>';
				}
				butin += '<span class="gallerybut">'+i+'</span>';
			}				
			butin += '</div><div class="gallbut_right"></div><div class="gallbut_pause"></div><div class="gallbut_play"></div><div class="gallbut_close"></div>';
			var gallin = '<div class="gallery_left"><div class="gallbutcontainer">'+butin+'</div></div><div class="gallery_right">'+addin+'</div>';				
			$('#page_gallery').html(gallin);
			
			var ntitle = $(xdata).find('project').eq(pos).find('title').text();
				$('#bottomlefttext').stop(true,true).hide();
				$('#bottomlefttext').html(ntitle);
				$('#bottomlefttext').stop(true,true).fadeIn(300);
			
			setGalleryButtons();
			galleryShift(1);
			startGallery();
			$('#page_gallery').stop(true,true).fadeIn();
			break;
		case "#page_location":
			
			galleryamt = $(xdata).find('project').eq(pos).find('gallery').find('pic').length;
			gallerybutgrpamt = Math.ceil(galleryamt/gallerybutgrp);
			gallerycurgrp = 1;
			var imgpath = mainimgpath+'architecture/gallery/';
			var bigimgpath = mainimgpath+'architecture/gallery/big/';
			var addin = '';
			var butin = '<div class="gallbut_left"></div><div class="gallbut_grp">';
			for(var i =1;i<=galleryamt;i++){
				if($(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).attr('bigimage') == 'yes'){
					addin+= '<div class="galleryimg"><a rel="prettyPhoto[projecttype]" href="'+bigimgpath+$(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).text()+'" ><img src="'+imgpath+$(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).text()+'" alt="'+$(xdata).find('project').eq(pos).find('title').text()+'"/></a></div>';
				} else {
					addin+= '<div class="galleryimg"><img src="'+imgpath+$(xdata).find('project').eq(pos).find('gallery').find('pic').eq(i-1).text()+'"/></div>';
				}
				butin += '<span class="gallerybut">'+i+'</span>';
			}				
			butin += '</div><div class="gallbut_right"></div><div class="gallbut_pause"></div><div class="gallbut_play"></div><div class="gallbut_close"></div>';
			var gallin = '<div class="gallery_left"><div class="gallbutcontainer">'+butin+'</div></div><div class="gallery_right">'+addin+'</div>';				
			$('#page_gallery').html(gallin);
			
			var ntitle = $(xdata).find('project').eq(pos).find('title').text();
				$('#bottomlefttext').stop(true,true).hide();
				$('#bottomlefttext').html(ntitle);
				$('#bottomlefttext').stop(true,true).fadeIn(300);
			
			setGalleryButtons();
			galleryShift(1);
			startGallery();
			$('#page_gallery').stop(true,true).fadeIn();
			break;
	}
	 $("a[rel^='prettyPhoto']").prettyPhoto({
		theme:'light_square',
		gallery_markup: '',
		opacity: 1,
		horizontal_padding: 10,
		deeplinking: false,
		markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_topclose"><a class="pp_close" href="#">Close</a></div> \
						<div class="pp_top"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
						<div class="pp_content_container"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">next</a> \
											<a class="pp_previous" href="#">previous</a> \
										</div> \
										<div id="pp_full_res"></div> \
										<div class="pp_details"> \
										</div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
						<div class="pp_bottom"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>'
	});
	 $("a[rel^='prettyPhoto']").click(function(){
		$('.gallbut_play').show();
		$('.gallbut_pause').hide();
		clearInterval(gallerytimeInt);
	 });
}


function setGalleryButtons() {
	if(gallerybutgrpamt > 1){
		$('.gallbut_left').hover(function(){
			$(this).css('background-position', 'top right');
		},function(){
			$(this).css('background-position', 'top left');
		});
		$('.gallbut_left').click(function(){
			var newgrp =gallerycurgrp -1;
			if(newgrp <1){
				newgrp = 1;
			}
			if(newgrp != gallerycurgrp){
				$('.gallerybut').stop(true,true).hide();
				for(var z=1;z<=7;z++){
					var gbnode = (z+((newgrp-1)*7)-1);
					$('.gallerybut').eq(gbnode).stop(true,true).fadeIn(300);
				}
				gallerycurgrp = newgrp;
			}
		});
		$('.gallbut_right').hover(function(){
			$(this).css('background-position', 'top right');
		},function(){
			$(this).css('background-position', 'top left');
		});
		$('.gallbut_right').click(function(){
			var newgrp =gallerycurgrp +1;
			if(newgrp >gallerybutgrpamt ){
				newgrp = gallerybutgrpamt;
			}
			if(newgrp != gallerycurgrp){
				$('.gallerybut').stop(true,true).hide();
				for(var z=1;z<=7;z++){
					var gbnode = (z+((newgrp-1)*7)-1);
					$('.gallerybut').eq(gbnode).stop(true,true).fadeIn(300);
				}
				gallerycurgrp = newgrp;
			}
		});
		
		
	} else {
		$('.gallbut_left').hide();
		$('.gallbut_right').hide();
	}
	$('.gallerybut').hover(function(){
			$(this).addClass('gallerybuthover');
		},function(){
			$(this).removeClass('gallerybuthover');
		});
		$('.gallerybut').click(function(){
			var index = $(this).index()+1;
			galleryShift(index);
		});
	$('.gallbut_pause').hover(function(){
		$(this).css('background-position', 'top right');
	},function(){
		$(this).css('background-position', 'top left');
	});
	
	$('.gallbut_pause').click(function(){
		$(this).hide();
		$('.gallbut_play').show();
		clearInterval(gallerytimeInt);
	});
	
	$('.gallbut_play').hover(function(){
		$(this).css('background-position', 'top right');
	},function(){
		$(this).css('background-position', 'top left');
	});
	
	$('.gallbut_play').click(function(){
		$(this).hide();
		$('.gallbut_pause').show();
		clearInterval(gallerytimeInt);
		startGallery();
	});
	if($('.gallbut_close').size()>0){
		$('.gallbut_close').hover(function(){
			$(this).css('background-position', 'top right');
		},function(){
			$(this).css('background-position', 'top left');
		});
		
		$('.gallbut_close').click(function(){
			dropGallery();
		});
	}
}

function dropGallery(){
	clearInterval(gallerytimeInt);
	$('#page_gallery').empty();
	$('#page_gallery').stop(true,true).fadeOut(500,function(){
		
	});
	isinnergallery = 0;
}

function startGallery() {
	gallerytimeInt = setInterval(galleryNext, 4000);
}

function galleryNext(){
	var pos = gallerycur+1;
	if(pos > galleryamt){
		pos = 1;
	}
	galleryShift(pos);
}


function galleryShift(pos) {
	$('.galleryimg').stop(true,true).fadeOut(300);
	$('.galleryimg').eq(pos-1).stop(true,true).fadeIn(400);
	$('.gallerybut').removeClass('gallbutactive');
	$('.gallerybut').eq(pos-1).addClass('gallbutactive');
	
	if(gallerybutgrpamt > 1){
		//position switch
		var curgrp = Math.ceil(pos/7);
		if(curgrp != gallerycurgrp){
				$('.gallerybut').stop(true,true).hide();
				for(var z=1;z<=7;z++){
					var gbnode = (z+((curgrp-1)*7)-1);
					$('.gallerybut').eq(gbnode).stop(true,true).fadeIn(300);
				}
				gallerycurgrp = curgrp;
			}
	}
	
	gallerycur = pos;
}

function dropAllContent(){
	gallerycur = 0;
	galleryamt = 0;
	gallerybutgrp = 7;
	gallerybutgrpamt = 0;
	gallerycurgrp = 0;
	clearInterval(gallerytimeInt);
	$('#page_gallery').empty();
	$('.gridbutton').removeClass('gridactive');
	$('.gridbutton .gridbg').stop(true,true).hide();
	$('.content_panel').empty();
	isinnergallery = 0;
	$('#bottomlefttext').empty();
	$('#bottomlefttext').stop(true,true);
	$('.faniimg').stop(true,true).hide();
	introcur = 1;
	clearInterval(introint);
}

function startIntroLines(){
	if(initialSet == 1){
		$('#topnavigation').hide();
		$('#logo').hide();
		$('#bottomnavigation').hide();
		$('#languageselector').hide();
	}

	$('#page_main .linea_1').css('top', '300px');
	$('#page_main .linea_2').css('top', '300px');
	$('#page_main .linea_3').css('top', '-300px');
	$('#page_main .linea_4').css('top', '-300px');
	$('#page_main .linea_5').css('left', '926px');
	$('#page_main .linea_6').css('left', '926px');
	$('#page_main .linea_1').animate({opacity:0},50);
	$('#page_main .linea_2').animate({opacity:0},50);
	$('#page_main .linea_3').animate({opacity:0},50);
	$('#page_main .linea_4').animate({opacity:0},50);
	$('#page_main .linea_5').animate({opacity:0},50);
	$('#page_main .linea_6').animate({opacity:0},50);

	$('#page_main .linea_6').delay(50).animate({
		left:0,
		opacity:1
	},1800);
	$('#page_main .linea_1').delay(50+600).animate({
		top:0,
		opacity:1
	},2000);
	$('#page_main .linea_3').delay(50+900).animate({
		top:0,
		opacity:1
	},2000);
	$('#page_main .linea_2').delay(50+1100).animate({
		top:0,
		opacity:1
	},2000);
	$('#page_main .linea_4').delay(50+1400).animate({
		top:179,
		opacity:1
	},1800,function(){
		startIntroImg();
	});
	$('#page_main .linea_5').delay(50+900).animate({
		left:179,
		opacity:1
	},2000,function(){
		if(initialSet == 1){
			$('#topnavigation').fadeIn(900);
			$('#logo').fadeIn(900);
			$('#bottomnavigation').fadeIn(900);
			$('#languageselector').fadeIn(900);
			initialSet = 0;
		}
	});
}


function startIntroImg() {
	//introint, introcur
	introcur = 1;
	isintro = 1;
	clearInterval(introint);
	for(var i =1;i<=5;i++){
		var clip = $('#page_main .frontani').eq((introseq[i-1])-1);
		if(i==5){
			clip.find('.faniimg').eq(introcur-1).stop(true,true).delay((i-1)*400).fadeIn(1200,function(){
				if(isintro == 1){
					introint = setTimeout(contIntroImg, 3000);
				}
			});
		} else {
			clip.find('.faniimg').eq(introcur-1).stop(true,true).delay((i-1)*400).fadeIn(1200);
		}
	}
	if(newsflag == 1){
		var link = $(newsdata).find('news').find('current').find('node').eq(0).find('link').text();
		var txt = $(newsdata).find('news').find('current').find('node').eq(0).find('title').text();
		var news = '<a href="'+link+'" target="_blank">'+txt+'</a>';
		
		$('#bottomlefttext').stop(true,true).hide();
		$('#bottomlefttext').html(news);
		$('#bottomlefttext').delay(500).fadeIn(1500);
	}
	
}

function contIntroImg() {
	clearInterval(introint);
	for(var i =1;i<=5;i++){
		var clip = $('#page_main .frontani').eq((introseq[i-1])-1);
		clip.find('.faniimg').eq(introcur-1).stop(true,true).delay((i-1)*400).fadeOut(1200);
	}
	introcur++;
	if(introcur > 4) {
		introcur =1;
	}
	for(var z =1;z<=5;z++){
		var clip = $('#page_main .frontani').eq((introseq[z-1])-1);
		if(z==5){
			clip.find('.faniimg').eq(introcur-1).stop(true,true).delay(2000+((z-1)*400)).fadeIn(1200,function(){
				if(isintro == 1){
					introint = setTimeout(contIntroImg, 3000);
				}
			});
		} else {
			clip.find('.faniimg').eq(introcur-1).stop(true,true).delay(2000+((z-1)*400)).fadeIn(1200);
		}
	}
	if(newsflag == 1){
		var link = $(newsdata).find('news').find('current').find('node').eq(introcur-1).find('link').text();
		var txt = $(newsdata).find('news').find('current').find('node').eq(introcur-1).find('title').text();
		var news = '<a href="'+link+'" target="_blank">'+txt+'</a>';
		
		$('#bottomlefttext').stop(true,true).fadeOut(1000,function(){
			$('#bottomlefttext').html(news);
		});
		$('#bottomlefttext').delay(2000).fadeIn(1500);
	}
}


$(function() {
	var winht = $(window).height();
	var shiftht = 0.6*(winht-350);
	if(winht - shiftht < 350){
		shiftht = winht - 350;
	}
	shiftht = shiftht ;
	$('#header').css('margin-top',shiftht+'px');
	//Navigation
	
	
	if($('#topnavigation').size()>0){
		//get widths
		$('#toplevelnavi .topbut').each(function(){
			var ind = $(this).parent('li').index();
			if($(this).parent('li').children('ul').size()>0){
				tmenuwidth[ind] = $(this).outerWidth();
				tsubmenuwidth[ind] = $(this).parent('li').children('ul').outerWidth();
			} else {
				tmenuwidth[ind] = $(this).outerWidth();
				tsubmenuwidth[ind] = 0;
			}
			$(this).parent('li').children('ul').css({'display':'none'});
		});
		
		//set position
		var tcurlocx = 0;
		$('#toplevelnavi .lvl1navi').each(function(){
			var pos = $(this).index();
			$(this).css({'left':tcurlocx});
			tcurlocx += tmenuwidth[pos];
		});
		
		$('#toplevelnavi .topbut').click(function(){
			//if($(this).parent('li').children('ul').size()>0){
				var pos = $(this).parent('li').index();
				$('.lvl1navi ul').stop(true).hide();
				navigationShift(1);
				
				/*$(this).parent('li').children('ul').stop(true).delay(100).animate({
					'opacity': 'toggle'
				},600);
				*/
			//} 
			return false;
		});
		
	}
	
	
	if($('#bottomnavigation').size()>0){
		
		//get widths
		$('#bottomlevelnavi .topbut').each(function(){
			var ind = $(this).parent('li').index();
			if($(this).parent('li').children('ul').size()>0){
				menuwidth[ind] = $(this).outerWidth();
				submenuwidth[ind] = $(this).parent('li').children('ul').outerWidth();
			} else {
				menuwidth[ind] = $(this).outerWidth();
				submenuwidth[ind] = 0;
			}
			$(this).parent('li').children('ul').css({'display':'none'});
		});
		
		//set position
		var curlocx = 0;
		$('#bottomlevelnavi .lvl1navi').each(function(){
			var pos = $(this).index();
			$(this).css({'left':curlocx});
			curlocx += menuwidth[pos];
		});
		$('#bottomlevelnavi').css('margin-left', 916-curlocx);
		$('#bottomlevelnavi .topbut').click(function(){
			//if($(this).parent('li').children('ul').size()>0){
				var pos = $(this).parent('li').index();
				navigationShift(pos);
				$('.lvl1navi ul').stop(true).hide();
				$(this).parent('li').children('ul').stop(true).delay(100).animate({
					'opacity': 'toggle'
				},600);
				
			//} 
			return false;
		});

	}

	$('.lvl1navi').hover(function(){
		$(this).find('.navidot').css('background-position', 'top right');
	},function(){
		$(this).find('.navidot').css('background-position', 'top left');
	});
	
	$('.lvl1navi a').click(function(){
		if($(this).hasClass('innerlink')){
			var nextpage = $(this).attr('href');
			$('.pagecontent').stop(true,true).hide();
			$('.topbut').removeClass('topbutactive');
			
			dropAllContent();
			isintro = 0;
			switch(nextpage) {
				case '#page_profile':
					loadXML('#page_profile','xmldata/profile.xml','profile');
					$(this).addClass('topbutactive');
					break;
				case '#page_people':
					loadXML('#page_people','xmldata/peoplestudio.xml','people');
					$(this).addClass('topbutactive');
					break;
				case '#page_contact':
					$(nextpage).stop(true,true).fadeIn();
					$(this).addClass('topbutactive');
					break;
				case '#page_careers':
					loadXML('#page_careers','xmldata/careers.xml','careers');
					$(this).addClass('topbutactive');
					break;
				case "#page_projecttype":
					loadXML('#page_projecttype','xmldata/gallery.xml','projecttype');
					$(this).parentsUntil('.lvl1navi').find('.topbut').addClass('topbutactive');
					break;
				case "#page_location":
					loadXML('#page_locationmap','xmldata/gallery-location.xml','location');
					$(this).parentsUntil('.lvl1navi').find('.topbut').addClass('topbutactive');
					break;
				case "#page_interiors":
					loadXML('#page_gallery','xmldata/interiors.xml','interior');
					$(this).addClass('topbutactive');
					//$('#page_gallery').stop(true,true).fadeIn();
					break;
				case "#page_collection":
					loadXML('#page_gallery','xmldata/collection.xml','collection');
					$(this).addClass('topbutactive');
					//$('#page_gallery').stop(true,true).fadeIn();
					break;
				case "#page_landscape":
					loadXML('#page_gallery','xmldata/landscape.xml','landscape');
					$(this).addClass('topbutactive');
					//$('#page_gallery').stop(true,true).fadeIn();
					break;
				
				default:
					//$(nextpage).stop(true,true).fadeIn();
			}
			
			return false;
		} else {
			var url = $(this).attr('href');
			if(url != '#'){
				window.open(url);
			}
			//return true;
		}
		
		
		
	});
	$('#logo').click(function(){
		$('.pagecontent').stop(true,true).hide();
		dropAllContent();
		$('#page_main').stop(true,true).fadeIn();
		 startIntroLines();
		return false;
	});
	//panels
	//setupNavPanels('#page_main .gridbutton_container');
	setupNavPanels('#page_profile .gridbutton_container');
	setupNavPanels('#page_people .gridbutton_container');
	setupNavPanels('#page_careers .gridbutton_container');
	setupNavPanels('#page_projecttype .gridbutton_container');
	
	//populate locationlist
	var loclist = '<ul>';
	var locsplit = Math.ceil(countrybutlist.length/2);
	for(var q=1;q<=countrybutlist.length;q++){
		loclist += '<li><a class="locbut" href="#">'+countrybutlist[q-1]+'</a></li>';
		if(q == locsplit){
			loclist += '</ul><ul>';
		}
	}
	loclist += '</ul>';
	$('#locationbuttonlisting').html(loclist);
	
	$('.locbut').hover(function(){
		var ind = $(this).index('.locbut');
		var str = countrybutlist[ind];
		str = str.replace('&','');
		
		str = 'icon_'+str.replace('  ','_');
		str = str.replace(' ','_').toLowerCase();
		$('#'+str).css('background-position', 'top right');
	},function(){
		var ind = $(this).index('.locbut');
		var str = countrybutlist[ind];
		str = str.replace('&','');
		str = 'icon_'+str.replace('  ','_');
		str = str.replace(' ','_').toLowerCase();
		$('#'+str).css('background-position', 'top left');
	});
	
	$('.locbut').click(function(){
		var str = $(this).index('.locbut');
		if(locationflag == 1){
			gridClick('#page_location',str);
		}
		return false;
	});
	$('#locationmapbutback').click(function(){
		//reset
		$('#page_location').stop(true,true).fadeOut(function(){ $('#page_location .content_panel').empty(); $('#bottomlefttext').empty();});
		$('#page_locationmap').stop(true,true).fadeIn();
	});
	
	
	$('#page_main').stop(true,true).fadeIn();
	loadNews();
	 startIntroLines();
	
	//get location icons
	$('.locationicon').each(function(){
		var locind = parseInt($(this).css('left').replace('px',''));
		locationiconlist[$(this).index('.locationicon')] = locind;
	});
});
